import time
import pytest
import allure
import TestCase

from Base.InitDriver import init_driver
from Page.CommonPage import CommonPage
from Utils.XiaoMaUtils import XiaoMaUtils


@allure.feature('TestCase_06: 加入车队，输入正确的口令后，成功加入车队')
@allure.issue(
    'https://www.tapd.cn/56767367/sparrow/tcase/view/1156767367001059686?url_cache_key=ab49d6eabe9823050826460ca132aea4')
class TestCaseMotorcade06:
    def setup_class(self):
        self.driver = init_driver()
        # 实例化页面
        # 桌面
        self.launcher_index_page = CommonPage(self.driver).get_launcher_index_page()
        self.launcher_app_list_page = CommonPage(self.driver).get_launcher_app_list_page()
        # 车队
        self.motorcade_index_page = CommonPage(self.driver).get_motorcade_index_page()
        self.motorcade_details_page = CommonPage(self.driver).get_motorcade_details_page()
        self.motorcade_setting_page = CommonPage(self.driver).get_motorcade_setting_page()

    def teardown_class(self):
        XiaoMaUtils.close_app('motorcade')
        # XiaoMaUtils.clear_data('motorcade')
        self.driver.quit()

    @allure.story('step1:点击应用页')
    @pytest.mark.run(order=61)
    def test_motorcade_06_1_click_list(self):
        try:
            self.launcher_index_page.click_app_list()
            time.sleep(5)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step2: 点击列表中的车队图标")
    @pytest.mark.run(order=62)
    def test_motorcade_06_2_click_motorcade(self):
        try:
            self.launcher_app_list_page.click_appname(TestCase.MOTORCADE)
            time.sleep(5)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step3: 查看车队是否启动")
    @pytest.mark.run(order=63)
    def test_motorcade_06_3_is_display_motorcade(self):
        try:
            text1 = self.motorcade_index_page.is_display_motorcade_index()
            if text1 == False:
                self.motorcade_details_page.is_display_motorcade_details()
            else:
                print("当前车队首页无车队,请先创建车队")
                self.motorcade_index_page.click_index_create_car()
                self.motorcade_index_page.input_index_create(index='测试车队')
                self.motorcade_index_page.click_index_create_ok()
                self.motorcade_details_page.is_display_motorcade_details()
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step4: 点击加入车队按钮,输入车牌号,如果存在出现提示,否则加入成功")
    @pytest.mark.run(order=64)
    def test_motorcade_06_4_is_display_motorcade(self):
        try:
            text1 = self.motorcade_details_page.get_ele_text(self.motorcade_details_page.get_list_car_password())
            text3 = text1[3:]
            self.motorcade_details_page.click_add_list_car_explanation()
            self.motorcade_details_page.is_display_motorcade_add()
            text2 = 482225
            if text3 == text2:
                print("输入的车队已加入,请先另外的车机创建一个车队")
            else:
                self.motorcade_details_page.input_car_add_explanation(text2)
                self.motorcade_details_page.click_add_ok()
                text4 = self.motorcade_details_page.is_toast_exist("已加入该车队")
                print(text4)
                if text4 == 'True':
                    print("用例成功")
                else:
                    print("用例代码输入的车队已存在,请重新输入一个车队")
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)


if __name__ == 'main__':
    # pass
    pytest.main("-s testcase_motorcade_06.py")
